package alex.edu;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyMapActivity extends MapActivity {

	private MapView myMapView;
	MyItemizedOverlay overlay;
	ServerResponse serverResponse;
//	ArrayList<Country> countries_user = new ArrayList<Country>();

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.map_layout);
		myMapView = (MapView) findViewById(R.id.map_view);
		myMapView.setSatellite(true);
		myMapView.setStreetView(true);
		myMapView.setTraffic(true);

		Geocoder fwdGeocoder = new Geocoder(this, Locale.getDefault());
		String streetAddress = "Egypt";
		List<Address> locations = null;
		try {
			locations = fwdGeocoder.getFromLocationName(streetAddress, 3);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Double latSpan = locations.get(0).getLongitude() * 1E6;
		Double longSpan = locations.get(0).getLatitude() * 1E6;

		// Configuring and Using MapViews
		int y = 10;
		int x = 10;
		MapView.LayoutParams lp = new MapView.LayoutParams(
				MapView.LayoutParams.WRAP_CONTENT,
				MapView.LayoutParams.WRAP_CONTENT, x, y,
				MapView.LayoutParams.TOP_LEFT);
		// myMapView.getZoomButtonsController().getZoomControls();
		View zoomControls = myMapView.getZoomControls();
		myMapView.addView(zoomControls, lp);
		myMapView.displayZoomControls(true);

		// Using the Map Controller
		MapController mapController = myMapView.getController();
		mapController.setZoom(3);
		GeoPoint point = new GeoPoint(latSpan.intValue(), longSpan.intValue());
		mapController.setCenter(point);
		mapController.animateTo(point);

		// A special Overlay designed to show your current location
		// List<Overlay> overlays = myMapView.getOverlays();
		// MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,
		// myMapView);
		// overlays.add(myLocationOverlay);
		// myLocationOverlay.enableCompass();
		// myLocationOverlay.enableMyLocation();
		// overlays = myMapView.getOverlays();

		// initialize Item marker overlay
		overlay = new MyItemizedOverlay(getResources().getDrawable(
				R.drawable.marker), this);
		// initilazie server response
		serverResponse = new ServerResponse();
		// GetAllRecentStatuses
		getAllRecentStatuses();
		// mark all countries
		markAllCountries();
		// pin the map to display status of every country
	}

	private void getCountryStatus(String countryName) {
		serverResponse.getAllStatus(getString(R.string.country_server)
				+ "?country=" + countryName);
	}

	private void pinCountry(GeoPoint geoPoint, String countryName) {
		MapView.LayoutParams geoLP;
		geoLP = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
				MapView.LayoutParams.WRAP_CONTENT, geoPoint,
				MapView.LayoutParams.TOP_LEFT);
		EditText editText = new EditText(getApplicationContext());
		// set editText with country status
		getCountryStatus(countryName);
		// it is only one country
		for (int i = 0; i < serverResponse.country_user.size(); i++) {
			// add user status to the edit box
			editText.append(serverResponse.country_user.get(0).users.get(i)
					.toString());
		}
		editText.setText("Location Pinned");
		myMapView.addView(editText, geoLP);
	}

	private void markAllCountries() {
		for (int i = 0; i < serverResponse.countries_user.size(); i++) {
			markCountry(serverResponse.countries_user.get(i).countryName);
		}
	}

	private void getAllRecentStatuses() {
		serverResponse.getAllStatus(getString(R.string.country_server));
	}

	public void markCountry(String countryName) {
		Geocoder fwdGeocoder = new Geocoder(this, Locale.getDefault());
		String streetAddress = countryName;
		List<Address> locations = null;
		try {
			locations = fwdGeocoder.getFromLocationName(streetAddress, 3);
		} catch (IOException e) {
			e.printStackTrace();
		}

		Double latSpan = locations.get(0).getLongitude() * 1E6;
		Double longSpan = locations.get(0).getLatitude() * 1E6;
		GeoPoint geoPoint = new GeoPoint(latSpan.intValue(), longSpan
				.intValue());
		// pin this country on map
//		pinCountry(geoPoint, countryName);
		// find country geopoint
		OverlayItem item = new OverlayItem(geoPoint, countryName, null);
		overlay.addItem(item);
		myMapView.getOverlays().add(overlay);
		// move to location
		myMapView.getController().animateTo(geoPoint);
		// redraw map
		myMapView.postInvalidate();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

}
